//
//  StatusListViewModel.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/28.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import Foundation

class StatusListViewModel {
    
    lazy var statuses = [Status]()
    
    func loadStatus(isPullup: Bool, finished:(isSuccess: Bool)->()) {
        
        let since_id = isPullup ? 0 : (statuses.first?.id ?? 0)
        let max_id = isPullup ? (statuses.last?.id ?? 0) : 0
        
        // MARK:- 使用网络工具加载微博数据
        NetworkTools.sharedTools.loadStatus(since_id, max_id: max_id) { (result) -> () in
            
            guard let result = result else {
                print("你的网络不给力")
                finished(isSuccess: false)
                return
            }
            
            guard let array = result["statuses"] as? [[String: AnyObject]] else {
                print("网络数据出错")
                finished(isSuccess: false)
                return
            }
            
            // MARK:- 字典转模型
            var arrM = [Status]()
            
            for dict in array {
                arrM.append(Status.yy_modelWithJSON(dict))
            }
            
            print("刷新\(arrM.count)条")
            
            if isPullup {
                self.statuses += arrM
            } else {
                self.statuses = arrM + self.statuses
            }
            
            finished(isSuccess: true)
        }
    }
}